package com.rf.richfitwheel.workflow.service.impl;

import com.rf.richfitwheel.admin.feignclient.FSysUserService;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.workflow.dao.WfProcessTaskMapper;
import com.rf.richfitwheel.workflow.model.WfProcessTask;
import com.rf.richfitwheel.workflow.model.WfProcessTaskExample;
import com.rf.richfitwheel.workflow.service.ModelService;
import com.rf.richfitwheel.workflow.service.WfProcessTaskService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.batik.transcoder.TranscoderException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("wfProcessTaskService")
public class WfProcessTaskServiceImpl implements WfProcessTaskService {
    @Autowired
    private WfProcessTaskMapper wfProcessTaskMapper;

    @Autowired
    private ModelService modelService;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public WfProcessTask findByProDefIdAndTaskDefKey(String proDefId, String taskDefKey) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andProcDefIdEqualTo(proDefId);
        criteria.andTaskDefKeyEqualTo(taskDefKey);
        example.setOrderByClause(" task_def_key");
        List<WfProcessTask> wfProcessTasks = wfProcessTaskMapper.selectByExample(example);
        if (wfProcessTasks.size() > 0) {
            return wfProcessTasks.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void updateWfProcessTask(WfProcessTask wfProcessTask) {
        wfProcessTaskMapper.updateByPrimaryKeySelective(wfProcessTask);
    }

    @Override
    public void addWfProcessTask(WfProcessTask userTask) {
        wfProcessTaskMapper.insert(userTask);
    }

    @Override
    public void updateWfProcessTasks(List<WfProcessTask> wfProcessTasks) {
        for (WfProcessTask wfProcessTask : wfProcessTasks) {
            this.updateWfProcessTask(wfProcessTask);
        }
    }

    @Override
    public void deleteByProDefId(String proDefId) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andProcDefIdEqualTo(proDefId);
        wfProcessTaskMapper.deleteByExample(example);
    }


    @Override
    public List<WfProcessTask> findByProDefKey(String proDefKey) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andProcDefKeyEqualTo(proDefKey);
        example.setOrderByClause(" task_def_key");
        List<WfProcessTask> WfProcessTasks = wfProcessTaskMapper.selectByExample(example);
        return WfProcessTasks;
    }

    @Override
    public void updateWfProcessTasksAndPublish(String modelId, List<WfProcessTask> wfProcessTasks, UserVO userVO) throws TranscoderException, IOException {
        Map<String, WfProcessTask> wfProcessTaskMap = new HashMap<>();
        for (WfProcessTask wfProcessTask : wfProcessTasks) {
            wfProcessTaskMap.put(wfProcessTask.getTaskDefKey(), wfProcessTask);
        }
        modelService.editMode(modelId, wfProcessTaskMap);
        String deploymentId = modelService.deploy1(modelId, userVO);
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).latestVersion().singleResult();
        for (WfProcessTask wfProcessTask : wfProcessTasks) {
            wfProcessTask.setProcDefId(processDefinition.getId());
            this.updateWfProcessTask(wfProcessTask);
        }
    }

    @Override
    public WfProcessTask getByStationProcessOrderId(String stationProcessOrderId) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andStationProcessOrderIdEqualTo(stationProcessOrderId);
        List<WfProcessTask> WfProcessTasks = wfProcessTaskMapper.selectByExample(example);
        if (WfProcessTasks.size() > 0) {
            return WfProcessTasks.get(0);
        }
        return null;
    }

    @Override
    public void deleteByProDefKey(String key) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andProcDefKeyEqualTo(key);
        wfProcessTaskMapper.deleteByExample(example);
    }

    @Override
    public List<WfProcessTask> findByProDefId(String proDefId) {
        WfProcessTaskExample example = new WfProcessTaskExample();
        WfProcessTaskExample.Criteria criteria = example.createCriteria();
        criteria.andProcDefIdEqualTo(proDefId);
        example.setOrderByClause(" task_def_key");
        List<WfProcessTask> WfProcessTasks = wfProcessTaskMapper.selectByExample(example);
        return WfProcessTasks;
    }

}